package com.service.admin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.service.admin.admin.controller.request.RoleMenuReq;
import com.service.admin.admin.controller.request.RoleReq;
import com.service.admin.admin.service.SysRoleService;
import com.service.core.exception.CommonException;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.SysMenuMapper;
import com.service.dao.mapper.SysRoleMapper;
import com.service.dao.mapper.SysRoleMenuMapper;
import com.service.dao.model.SysRole;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年08月02日</dd>
 * </dl>
 *
 * @author
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysMenuMapper menuMapper;
    @Autowired
    private SysRoleMenuMapper menuRoleMapper;

    @Override
    public List<Integer> getMenusIdByRid(Integer rid) {
        List<Integer> menuId = menuRoleMapper.getMenusIdByRid(rid);
        return menuId.stream().filter(x -> {
            Map<String, Object> queryParam = Maps.newHashMap();
            queryParam.put("parent_id", x);
            queryParam.put("enabled", true);
            return ObjectUtils.isEmpty(menuMapper.selectByMap(queryParam));
        }).collect(Collectors.toList());
    }

    @Override
    public List<SysRole> listSysRole() {
        return list(new QueryWrapper<SysRole>().lambda()
                .eq(SysRole::getRoleStatus, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    @Transactional
    public void saveRole(RoleReq roleReq) {
        SysRole sysRole = ModelMapperUtil.getStrictModelMapper().map(roleReq, SysRole.class);
        if (ObjectUtils.isEmpty(roleReq.getId())) {

            Map<String, Object> queryPram = Maps.newHashMap();
            queryPram.put("name", roleReq.getName());
            List<SysRole> sysRoleList = baseMapper.selectByMap(queryPram);
            if (!ObjectUtils.isEmpty(sysRoleList)) {
                throw new CommonException("角色名称已存在");
            }
            baseMapper.insert(sysRole);
        } else {
            baseMapper.updateById(sysRole);
        }
    }

    @Override
    @Transactional
    public void saveRoleMenu(RoleMenuReq roleMenuReq) {

        deleteRoleMenu(roleMenuReq.getRoleId());
        List<Integer> menuIds = roleMenuReq.getMenuIdList();
        if (!ObjectUtils.isEmpty(menuIds)) {
            menuRoleMapper.insertBatch(roleMenuReq.getMenuIdList(), roleMenuReq.getRoleId());
        }
    }

    private void deleteRoleMenu(Integer roleId) {
        Map<String, Object> delParm = Maps.newHashMap();
        delParm.put("rid", roleId);
        menuRoleMapper.deleteByMap(delParm);
    }

    @Override
    @Transactional
    public void deleteRole(Integer roleId) {
        SysRole role = baseMapper.selectById(roleId);
        if (!ObjectUtils.isEmpty(role)) {
            role = new SysRole();
            role.setId(roleId);
            role.setRoleStatus(false);
            baseMapper.updateByPrimaryKeySelective(role);
            deleteRoleMenu(roleId);
        }
    }
}
